package com.amazon.ags.client.whispersync;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;
import com.amazon.ags.api.ErrorCode;
import com.amazon.ags.api.whispersync.RevertBlobCallback;
import com.amazon.ags.api.whispersync.RevertCallback;
import com.amazon.ags.api.whispersync.RevertMultiFileCallback;
import com.amazon.ags.api.whispersync.SynchronizeBlobCallback;
import com.amazon.ags.api.whispersync.SynchronizeBlobProgressRequest;
import com.amazon.ags.api.whispersync.SynchronizeBlobRequest;
import com.amazon.ags.api.whispersync.SynchronizeCallback;
import com.amazon.ags.api.whispersync.SynchronizeMultiFileCallback;
import com.amazon.ags.api.whispersync.SynchronizeMultiFileProgressRequest;
import com.amazon.ags.api.whispersync.SynchronizeMultiFileRequest;
import com.amazon.ags.api.whispersync.WhisperSyncClient;
import com.amazon.ags.client.AmazonGamesService;
import com.amazon.ags.client.whispersync.savedgame.FileBackedSummaryRepository;
import com.amazon.ags.client.whispersync.savedgame.JsonSummaryMarshaller;
import com.amazon.ags.client.whispersync.savedgame.PendingDownload;
import com.amazon.ags.client.whispersync.savedgame.PendingUpload;
import com.amazon.ags.client.whispersync.savedgame.SummaryMarshaller;
import com.amazon.ags.client.whispersync.savedgame.SummaryRepository;
import com.amazon.ags.constants.whispersync.ConflictStrategy;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.Date;

public final class WhisperSyncServiceProxy
  implements WhisperSyncClient
{
  private static final ConflictStrategy DEFAULT_CONFLICT_STRATEGY = ConflictStrategy.PLAYER_SELECT;
  private static final String FEATURE_NAME = "STC";
  private static final String TAG = "STC_" + WhisperSyncServiceProxy.class.getSimpleName();
  private final AmazonGamesService amazonGamesService;
  private final Handler apiHandler;
  private final Context context;
  private GameSavePackager gamePackager;
  private final SummaryMarshaller summaryMarshaller;
  private final SummaryRepository summaryRepository;

  public WhisperSyncServiceProxy(Context paramContext, AmazonGamesService paramAmazonGamesService, Handler paramHandler)
  {
    this.context = paramContext;
    this.amazonGamesService = paramAmazonGamesService;
    this.apiHandler = paramHandler;
    this.gamePackager = new GameSavePackager(paramContext);
    this.summaryMarshaller = new JsonSummaryMarshaller();
    this.summaryRepository = new FileBackedSummaryRepository(paramContext, this.summaryMarshaller);
  }

  private Bundle createRequestBundleWithData(byte[] paramArrayOfByte, String paramString)
  {
    Bundle localBundle = new Bundle();
    localBundle.putByteArray("STC_DATA", paramArrayOfByte);
    localBundle.putString("STC_DESCRIPTION", paramString);
    return localBundle;
  }

  private GameSavePackager getPackager(SynchronizeMultiFileProgressRequest paramSynchronizeMultiFileProgressRequest)
  {
    if (paramSynchronizeMultiFileProgressRequest.getFilter() != null)
      return new GameSavePackager(this.context, paramSynchronizeMultiFileProgressRequest.getFilter());
    return this.gamePackager;
  }

  private void requestRevert(RevertCallback paramRevertCallback, Handler paramHandler)
  {
    if (!this.amazonGamesService.isReady())
    {
      paramRevertCallback.onRevertFailure(ErrorCode.SERVICE_NOT_READY);
      return;
    }
    Message localMessage = Message.obtain();
    localMessage.what = 22;
    localMessage.replyTo = new Messenger(paramHandler);
    localMessage.setData(new Bundle());
    try
    {
      this.amazonGamesService.sendMessage(localMessage);
      return;
    }
    catch (RemoteException localRemoteException)
    {
      Log.e(TAG, "Unable to send Message to Service: ", localRemoteException);
      paramRevertCallback.onRevertFailure(ErrorCode.UNRECOVERABLE);
    }
  }

  private void synchronizeInternal(Handler paramHandler, SynchronizeCallback paramSynchronizeCallback, ConflictStrategy paramConflictStrategy)
  {
    if (paramSynchronizeCallback == null)
      throw new IllegalArgumentException("A Callback must be provided to synchronize");
    if (!this.amazonGamesService.isReady())
    {
      paramSynchronizeCallback.onSynchronizeFailure(ErrorCode.SERVICE_NOT_READY);
      return;
    }
    try
    {
      PendingUpload localPendingUpload = this.summaryRepository.retrievePendingUpload();
      Message localMessage = Message.obtain();
      localMessage.what = 21;
      localMessage.replyTo = new Messenger(paramHandler);
      Bundle localBundle = createRequestBundleWithData(localPendingUpload.getData(), localPendingUpload.getDescription());
      localBundle.putLong("SAVE_TIME", localPendingUpload.getSaveTime().getTime());
      GameSummary localGameSummary = this.summaryRepository.retrieveSummary();
      localBundle.putString("LOCAL_GAME_SUMMARY", this.summaryMarshaller.marshal(localGameSummary));
      localBundle.putString("CONFLICT_STRATEGY", paramConflictStrategy.toString());
      localMessage.setData(localBundle);
      try
      {
        this.amazonGamesService.sendMessage(localMessage);
        return;
      }
      catch (RemoteException localRemoteException)
      {
        Log.e(TAG, "Unable to send Message to Service: ", localRemoteException);
        paramSynchronizeCallback.onSynchronizeFailure(ErrorCode.UNRECOVERABLE);
        return;
      }
    }
    catch (IOException localIOException)
    {
      paramSynchronizeCallback.onSynchronizeFailure(ErrorCode.IO_ERROR);
    }
  }

  public final boolean hasNewMultiFileGameData()
  {
    return this.summaryRepository.hasPendingDownload();
  }

  public void requestRevert(final RevertBlobCallback paramRevertBlobCallback)
  {
    this.apiHandler.post(new Runnable()
    {
      public void run()
      {
        RevertBlobCallbackHandler localRevertBlobCallbackHandler = new RevertBlobCallbackHandler(paramRevertBlobCallback, WhisperSyncServiceProxy.this.summaryRepository);
        WhisperSyncServiceProxy.this.requestRevert(paramRevertBlobCallback, localRevertBlobCallbackHandler);
      }
    });
  }

  public void requestRevert(final RevertMultiFileCallback paramRevertMultiFileCallback)
  {
    this.apiHandler.post(new Runnable()
    {
      public void run()
      {
        RevertMultiFileCallbackHandler localRevertMultiFileCallbackHandler = new RevertMultiFileCallbackHandler(paramRevertMultiFileCallback, WhisperSyncServiceProxy.this.summaryRepository);
        WhisperSyncServiceProxy.this.requestRevert(paramRevertMultiFileCallback, localRevertMultiFileCallbackHandler);
      }
    });
  }

  public void setFilter(FilenameFilter paramFilenameFilter)
  {
    this.gamePackager.setFilter(paramFilenameFilter);
  }

  public void setPackager(GameSavePackager paramGameSavePackager)
  {
    this.gamePackager = paramGameSavePackager;
  }

  public void synchronize(final SynchronizeBlobCallback paramSynchronizeBlobCallback)
  {
    this.apiHandler.post(new Runnable()
    {
      public void run()
      {
        SynchronizeBlobCallbackHandler localSynchronizeBlobCallbackHandler = new SynchronizeBlobCallbackHandler(paramSynchronizeBlobCallback, WhisperSyncServiceProxy.this.summaryRepository);
        WhisperSyncServiceProxy.this.synchronizeInternal(localSynchronizeBlobCallbackHandler, paramSynchronizeBlobCallback, WhisperSyncServiceProxy.DEFAULT_CONFLICT_STRATEGY);
      }
    });
  }

  public void synchronize(final SynchronizeBlobRequest paramSynchronizeBlobRequest)
  {
    this.apiHandler.post(new Runnable()
    {
      public void run()
      {
        SynchronizeBlobCallback localSynchronizeBlobCallback = paramSynchronizeBlobRequest.getCallback();
        SynchronizeBlobCallbackHandler localSynchronizeBlobCallbackHandler = new SynchronizeBlobCallbackHandler(localSynchronizeBlobCallback, WhisperSyncServiceProxy.this.summaryRepository);
        WhisperSyncServiceProxy.this.synchronizeInternal(localSynchronizeBlobCallbackHandler, localSynchronizeBlobCallback, paramSynchronizeBlobRequest.getConflictStrategy());
      }
    });
  }

  public void synchronize(final SynchronizeMultiFileCallback paramSynchronizeMultiFileCallback)
  {
    this.apiHandler.post(new Runnable()
    {
      public void run()
      {
        SynchronizeMultiFileCallbackHandler localSynchronizeMultiFileCallbackHandler = new SynchronizeMultiFileCallbackHandler(paramSynchronizeMultiFileCallback, WhisperSyncServiceProxy.this.summaryRepository);
        WhisperSyncServiceProxy.this.synchronizeInternal(localSynchronizeMultiFileCallbackHandler, paramSynchronizeMultiFileCallback, WhisperSyncServiceProxy.DEFAULT_CONFLICT_STRATEGY);
      }
    });
  }

  public void synchronize(final SynchronizeMultiFileRequest paramSynchronizeMultiFileRequest)
  {
    this.apiHandler.post(new Runnable()
    {
      public void run()
      {
        SynchronizeMultiFileCallback localSynchronizeMultiFileCallback = paramSynchronizeMultiFileRequest.getCallback();
        SynchronizeMultiFileCallbackHandler localSynchronizeMultiFileCallbackHandler = new SynchronizeMultiFileCallbackHandler(localSynchronizeMultiFileCallback, WhisperSyncServiceProxy.this.summaryRepository);
        WhisperSyncServiceProxy.this.synchronizeInternal(localSynchronizeMultiFileCallbackHandler, localSynchronizeMultiFileCallback, paramSynchronizeMultiFileRequest.getConflictStrategy());
      }
    });
  }

  public void synchronizeProgress(final SynchronizeBlobProgressRequest paramSynchronizeBlobProgressRequest)
  {
    this.apiHandler.post(new Runnable()
    {
      public void run()
      {
        SynchronizeBlobCallback localSynchronizeBlobCallback = paramSynchronizeBlobProgressRequest.getCallback();
        byte[] arrayOfByte = paramSynchronizeBlobProgressRequest.getData();
        if (arrayOfByte == null)
        {
          Log.e(WhisperSyncServiceProxy.TAG, "No data to be saved");
          localSynchronizeBlobCallback.onSynchronizeFailure(ErrorCode.IO_ERROR);
          return;
        }
        PendingUpload localPendingUpload = new PendingUpload(arrayOfByte, paramSynchronizeBlobProgressRequest.getDescription());
        try
        {
          WhisperSyncServiceProxy.this.summaryRepository.storePendingUpload(localPendingUpload);
          SynchronizeBlobCallbackHandler localSynchronizeBlobCallbackHandler = new SynchronizeBlobCallbackHandler(localSynchronizeBlobCallback, WhisperSyncServiceProxy.this.summaryRepository);
          WhisperSyncServiceProxy.this.synchronizeInternal(localSynchronizeBlobCallbackHandler, localSynchronizeBlobCallback, paramSynchronizeBlobProgressRequest.getConflictStrategy());
          return;
        }
        catch (IOException localIOException)
        {
          localSynchronizeBlobCallback.onSynchronizeFailure(ErrorCode.IO_ERROR);
        }
      }
    });
  }

  public void synchronizeProgress(final SynchronizeMultiFileProgressRequest paramSynchronizeMultiFileProgressRequest)
  {
    this.apiHandler.post(new Runnable()
    {
      public void run()
      {
        SynchronizeMultiFileCallback localSynchronizeMultiFileCallback = paramSynchronizeMultiFileProgressRequest.getCallback();
        byte[] arrayOfByte;
        try
        {
          arrayOfByte = WhisperSyncServiceProxy.this.getPackager(paramSynchronizeMultiFileProgressRequest).pack();
          if (arrayOfByte == null)
          {
            Log.e(WhisperSyncServiceProxy.TAG, "No data to be saved");
            localSynchronizeMultiFileCallback.onSynchronizeFailure(ErrorCode.IO_ERROR);
            return;
          }
        }
        catch (IOException localIOException1)
        {
          Log.e(WhisperSyncServiceProxy.TAG, "Exception in asynchronous save: ", localIOException1);
          localSynchronizeMultiFileCallback.onSynchronizeFailure(ErrorCode.IO_ERROR);
          return;
        }
        PendingUpload localPendingUpload = new PendingUpload(arrayOfByte, paramSynchronizeMultiFileProgressRequest.getDescription());
        try
        {
          WhisperSyncServiceProxy.this.summaryRepository.storePendingUpload(localPendingUpload);
          SynchronizeMultiFileCallbackHandler localSynchronizeMultiFileCallbackHandler = new SynchronizeMultiFileCallbackHandler(localSynchronizeMultiFileCallback, WhisperSyncServiceProxy.this.summaryRepository);
          WhisperSyncServiceProxy.this.synchronizeInternal(localSynchronizeMultiFileCallbackHandler, localSynchronizeMultiFileCallback, paramSynchronizeMultiFileProgressRequest.getConflictStrategy());
          return;
        }
        catch (IOException localIOException2)
        {
          localSynchronizeMultiFileCallback.onSynchronizeFailure(ErrorCode.IO_ERROR);
        }
      }
    });
  }

  public final void unpackNewMultiFileGameData()
    throws IOException
  {
    PendingDownload localPendingDownload = this.summaryRepository.retrievePendingDownload();
    if (localPendingDownload != null)
    {
      this.gamePackager.unpack(localPendingDownload.getData());
      this.summaryRepository.removePendingDownload();
      this.summaryRepository.storeSummary(localPendingDownload.getSummary());
      Log.d(TAG, "Successfully unpacked new multi-file game data");
      return;
    }
    Log.d(TAG, "Tried to unpack, but no data exists.  Ignoring.");
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.amazon.ags.client.whispersync.WhisperSyncServiceProxy
 * JD-Core Version:    0.6.2
 */